interface RequestOPtionsH {//接口限制
    url: string,
    Type: string,
    Params: any | object,
    Content: string,
    Header?: string,
    Methods: string
}

export default async ({ url, Type = "Get", Params, Content = "Content-type", Header = "application/x-www-form-urlencoded", Methods = 'fetch', ...rest }: RequestOPtionsH) => {
    return new Promise((resolve, reject) => {
        let XmLHttp: any;
        // 创建对象 create object
        if (window.XMLHttpRequest) {
            XmLHttp = new XMLHttpRequest();
        } else {
            // IE5，IE4
            XmLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        let sendData: object | any = {};
        if (Type == 'POST') {
            sendData = JSON.stringify(Params);
        }
        // 初始化 init
        XmLHttp.open(Type, url, true)
        XmLHttp.setRequestHeader(Content, Header);
        // 发送 send
        XmLHttp.send(sendData);

        // 事件绑定  Event binding
        XmLHttp.onreadystatechange = (): void => {
            if (XmLHttp.readyState === 4) {
                if (XmLHttp.status >= 200 && XmLHttp.status < 300) {
                    let obj = XmLHttp.response

                    if (Object.prototype.toString.call(obj) != '[object Object]') {
                        obj = JSON.parse(obj);
                    }
                    resolve(obj)
                } else {
                    reject(XmLHttp.status)
                }
            }
        }
    })
}